package com.gxa.accommodation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.accommodation.dto.AccommodationRoomDTO;
import com.gxa.accommodation.dto.SelectRoomByRoomTypeDTO;
import com.gxa.accommodation.entity.AccommodationRoom;
import com.gxa.accommodation.mapper.AccommodationRoomMapper;
import com.gxa.accommodation.service.AccommodationRoomService;
import com.gxa.accommodation.vo.AccommodationRoomVO;
import com.gxa.common.base.RespResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class AccommodationRoomServiceImpl implements AccommodationRoomService {
    @Autowired
    AccommodationRoomMapper accommodationRoomMapper;

    //审核通过
    private static final Byte AUDIT_SUCCESS=0;
    //审核不通过
    private static final Byte AUDIT_FAILURE=1;


    @Override
    public RespResult addRoom(AccommodationRoomDTO accommodationRoomTO) {

        QueryWrapper<AccommodationRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_number",accommodationRoomTO.getRoomNumber()).eq("accommodation_id",accommodationRoomTO.getAccommodationId());
        AccommodationRoom res = accommodationRoomMapper.selectOne(queryWrapper);
        if (res == null){
            AccommodationRoom accommodationRoom = null;
            accommodationRoom = BeanUtil.copyProperties(accommodationRoomTO,AccommodationRoom.class);
            accommodationRoomMapper.insert(accommodationRoom);
            return RespResult.success("添加成功");
        }else {
            return RespResult.error("添加失败,房间已存在");
        }
    }

    @Override
    public RespResult showRoomList(Integer currentPage, Integer pageSize) {
        if (currentPage==null||pageSize==null||currentPage<=0||pageSize<=0){
            currentPage=1;
            pageSize=10;
        }
        Page<AccommodationRoomVO> accommodationRoomPage = new Page<>(currentPage,pageSize);

        Page<AccommodationRoomVO> accommodationRoomIPage = accommodationRoomMapper.selectByPage(accommodationRoomPage);


        return RespResult.success(accommodationRoomIPage);
    }

    /**
     * 根据订单编号和房间号修改住宿信息（安排住宿）
     * @param orderId 订单id
     * @param roomId 房间id
     * @return RespResult
     */
    @Override
    public RespResult arrangeRoomByOrder(Long orderId,Long roomId) {
        if (orderId!=null&&roomId!=null){
            accommodationRoomMapper.arrangeRoomByOrder(roomId);
            //修改订单中的房间id
            accommodationRoomMapper.updataOrderStatusCheckById(orderId,roomId);
            return RespResult.success("入住成功");
        }
        return RespResult.error("参数异常");
    }

    /**
     * 根据订单编号审核退款
     * @param orderId 订单id
     * @return RespResult
     */
    @Transactional
    @Override
    public RespResult refundByOrder(Long orderId,Byte audit) {
        if (orderId!=null){
            //修改订单中的状态id
            if (AUDIT_SUCCESS.equals(audit)){
                accommodationRoomMapper.updataOrderStatusOutMoneyById((byte)5,orderId);
                return RespResult.success("审核通过");
            }
            if (AUDIT_FAILURE.equals(audit)){
                accommodationRoomMapper.updataOrderStatusOutMoneyById((byte)4,orderId);
                return RespResult.success("审核不通过");
            }


        }
        return RespResult.error("参数异常");
    }

    /**
     * 根据订单编号和房间号修改住宿信息（退住住宿）
     * @param orderId 订单id
     * @param roomId 房间id
     * @return RespResult
     */
    @Transactional
    @Override
    public RespResult outRoomByOrder(Long orderId, Long roomId) {
        if (orderId!=null&&roomId!=null){
            accommodationRoomMapper.outRoomByOrder(roomId);
            //修改订单的状态
            accommodationRoomMapper.updataOrderStatusOutMoneyById((byte)7,orderId);
            return RespResult.success("退住成功");

        }
        return RespResult.error("参数异常");
    }

    /**
     *
     * 根据订单编号和房间号修改住宿信息（换房）
     * @param orderId 订单id
     * @param roomId1 房间id1
     * @param roomId2 房间id2
     * @return RespResult
     */
    @Transactional
    @Override
    public RespResult changeRoomByOrder(Long orderId, Long roomId1,Long roomId2) {
        if (orderId!=null&&roomId1!=null){
            //修改原房间的状态
            accommodationRoomMapper.outRoomByOrder(roomId1);
            //重新添加一个房间
            accommodationRoomMapper.arrangeRoomByOrder(roomId2);

            //修改订单中的房间号
            accommodationRoomMapper.updataOrderStatusCheckById(orderId,roomId2);
            return RespResult.success("换房成功");

        }
        return RespResult.error("参数异常");
    }

    @Override
    public RespResult showRoomListByOrderNumAndRoomSerial(SelectRoomByRoomTypeDTO selectRoomByRoomTypeDTO) {

        Page<SelectRoomByRoomTypeDTO> accommodationRoomPage = new Page<>(selectRoomByRoomTypeDTO.getCurrentPage(),selectRoomByRoomTypeDTO.getPageSize());
        Page<SelectRoomByRoomTypeDTO> roomList = accommodationRoomMapper.selectListByOrderNum(selectRoomByRoomTypeDTO,accommodationRoomPage);

        return RespResult.success(roomList);
    }


}
